package cn.iocoder.yudao.module.shop.info.api;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.region.RegionUtils;
import cn.iocoder.yudao.framework.region.enums.RegionDataSet;
import cn.iocoder.yudao.module.api.product.evaluation.ProductEvaluationApi;
import cn.iocoder.yudao.module.api.product.spu.ProductSpuApi;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.shop.category.service.ShopCategoryService;
import cn.iocoder.yudao.module.shop.info.dal.dataobject.ShopInfoDO;
import cn.iocoder.yudao.module.shop.info.service.ShopInfoService;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

/**
 * 店铺信息的 API 实现类
 *
 * @author 山野羡民（1032694760@qq.com）
 */
@Service
@Validated
public class ShopInfoApiImpl implements ShopInfoApi {

    @Resource
    private ShopInfoService shopInfoService;
    @Resource
    private ShopCategoryService shopCategoryService;

    @Resource
    @Lazy // 延迟加载，避免循环依赖
    private ProductSpuApi productSpuApi;
    @Resource
    @Lazy // 延迟加载，避免循环依赖
    private ProductEvaluationApi productEvaluationApi;

    @Override
    public Long getOwnerMemberUserId(Long id) {
        return shopInfoService.getOwnerMemberUserId(id);
    }

    @Override
    public boolean isOwnerByMemberUserId(Long memberUserId) {
        return shopInfoService.isOwnerByMemberUserId(memberUserId);
    }

    @Override
    public List<ShopInfoDTO> getShopInfoList(Collection<Long> ids) {
        List<ShopInfoDO> list = shopInfoService.getInfoList(ids, null);
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<ShopInfoDTO> dtos = BeanUtils.toBean(list, ShopInfoDTO.class);
        Set<Long> shopIds = convertSet(dtos, ShopInfoDTO::getId);
        Map<Long, Integer> spuCountMap = productSpuApi.getSpuCountMapByShopIds(shopIds);
        Map<Long, Integer> commentCountMap = productEvaluationApi.getCommentCountMapByShopIds(shopIds);
        dtos.forEach(dto -> {
            RegionUtils.autoAssignCounty(RegionDataSet.ONLY_LATEST, dto.getCountyCode(), dto);
            dto.setProductCount(spuCountMap.get(dto.getId()));
            dto.setCommentCount(commentCountMap.get(dto.getId()));
        });
        return dtos;
    }

    @Nullable
    @Override
    public ShopInfoDTO getShopInfo(Long id) {
        ShopInfoDO shopInfoDO = shopInfoService.getInfo(id);
        if (shopInfoDO == null) {
            return null;
        }
        ShopInfoDTO respDTO = BeanUtils.toBean(shopInfoDO, ShopInfoDTO.class);
        // 创建团购套餐等场景用到了主营类目编码
        respDTO.assignCategory(shopCategoryService.getCategory(shopInfoDO.getCategoryId()));
        return respDTO;
    }

    @Override
    public Set<Long> getShopIdsByDisabled() {
        return shopInfoService.getShopIdsByDisabled();
    }

    @Override
    public Set<Long> getShopIdsByO2O() {
        return shopInfoService.getShopIdsByO2O();
    }

    @Override
    public Set<Long> getShopIdsByCategoryCode(String categoryCode) {
        return shopInfoService.getShopIdsByCategoryCode(categoryCode);
    }

    @Override
    public Set<Long> getShopIdsByCategoryId(Long categoryId) {
        return shopInfoService.getShopIdsByCategoryId(categoryId);
    }

    @Override
    public Set<Long> getShopIdsByCountyCodes(Set<String> countyCodes) {
        return shopInfoService.getShopIdsByCountyCodes(countyCodes);
    }

    @Override
    public Set<Long> getShopIdsByCityCodes(Set<String> cityCodes) {
        return shopInfoService.getShopIdsByCityCodes(cityCodes);
    }

    @Override
    public void deleteShopInfoByMemberUserId(Long memberUserId) {
        shopInfoService.deleteInfoByMemberUserId(memberUserId);
    }

    @Override
    public void updateScoreWhenEvaluation(Long id) {
        shopInfoService.updateScoreWhenEvaluation(id);
    }

    @Override
    public ShopInfoDTO getShopInfoByMemberUserId(Long memberUserId) {
        ShopInfoDO shopInfoDO = shopInfoService.getShopInfoByMemberUserId(memberUserId);
        if (shopInfoDO == null) {
            return null;
        }
        return BeanUtils.toBean(shopInfoDO, ShopInfoDTO.class);
    }

    @Override
    public Long getShopIdByAdminUserId(Long adminUserId) {
        ShopInfoDO shopInfoDO = shopInfoService.getShopInfoByAdminUserId(adminUserId);
        if (shopInfoDO == null) {
            return null;
        }
        return shopInfoDO.getId();
    }

    @Override
    public Long getShopIdByMemberUserId(Long memberUserId) {
        return shopInfoService.getShopIdByMemberUserId(memberUserId);
    }

    @Override
    public double getServiceFeePercent(Long id) {
        return shopInfoService.getServiceFeePercent(id);
    }

}
